package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.product.pojo.param.PictureAddNewParam;
import cn.tedu.csmall.product.pojo.vo.PictureStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.service.IPictureService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Validated
@RequestMapping("/pictures")
@RestController
@Api(tags = "03. 图片管理模块")
public class PictureController {

    @Autowired
    private IPictureService pictureService;
    @Autowired
    private IAlbumService albumService;

    // http://localhost:9180/pictures/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加图片")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid PictureAddNewParam pictureAddNewParam) {
        log.debug("开始处理【添加图片】的请求，参数：{}", pictureAddNewParam);
        pictureService.addNewPicture(pictureAddNewParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/pictures/{id}/delete
    @PostMapping("/{pictureId:^\\d+$}/delete")
    @ApiOperation("根据id删除图片")
    @ApiOperationSupport(order = 200)
    public JsonResult addNew(@PathVariable Long pictureId) {
        log.debug("开始处理【根据id删除图片】的请求，参数：{}", pictureId);
        pictureService.deletePicture(pictureId);
        return JsonResult.ok();
    }

    // http://localhost:9180/pictures/{id}/setCover
    @PostMapping("/{id:^\\d+$}/setCover")
    @ApiOperation("设置封面图片")
    @ApiOperationSupport(order = 300)
    public JsonResult setCover(@Range(min = 1,message = "修改失败，请提交合法的ID值！")
            @PathVariable Long id) {
        log.debug("开始处理【设置封面图片】的请求，参数ID：{}", id);
        pictureService.updateSetCover(id);
        return JsonResult.ok();
    }

}
